//
//  UIColor+WXExtension.m
//  LibraryUtils
//
//  Created by 罗世雄 on 2019/12/24.
//  Copyright © 2019 XES. All rights reserved.
//

#import "UIColor+WXExtension.h"

@implementation UIColor (WXExtension)

+ (UIColor *)wx_colorWithHEXValue:(NSInteger)hexValue
{
    return [self wx_colorWithHEXValue:hexValue alpha:1.0f];
}

+ (UIColor *)wx_colorWithHEXValue:(NSInteger)hexValue alpha:(CGFloat)alpha
{
    return [UIColor colorWithRed:((CGFloat)((hexValue & 0xFF0000) >> 16)) / 255.0f
                           green:((CGFloat)((hexValue & 0xFF00) >> 8)) / 255.0f
                            blue:((CGFloat)(hexValue & 0xFF)) / 255.0f
                           alpha:alpha];
}

+ (UIColor *)wx_colorWithHEXString:(NSString *)hexString
{
    if (![hexString isKindOfClass:[NSString class]]) {
        return nil;
    }
    if ([hexString isEqualToString:@""]) {
        return nil;
    }
    if ([hexString characterAtIndex:0] != '#') {
        return nil;
    }
    unsigned rgbValue = 0;
    NSScanner *scanner = [NSScanner scannerWithString:hexString];
    if ([hexString characterAtIndex:0] == '#') {
        [scanner setScanLocation:1];
    }
    [scanner scanHexInt:&rgbValue];
    return [self wx_colorWithHEXValue:rgbValue];
}


#pragma mark - 16进制颜色转换
static inline NSUInteger hexStrToInt(NSString *str) {
    uint32_t result = 0;
    sscanf([str UTF8String], "%X", &result);
    return result;
}

static BOOL hexStrToRGBA(NSString *str,
                         CGFloat *r, CGFloat *g, CGFloat *b, CGFloat *a) {
    
    
    str = ({ // Trim blank characters (space and newline) in head and tail.
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        [[str stringByTrimmingCharactersInSet:set] uppercaseString];
        
    });
    
    if ([str hasPrefix:@"#"]) {
        str = [str substringFromIndex:1];
    } else if ([str hasPrefix:@"0X"]) {
        str = [str substringFromIndex:2];
    }
    
    NSUInteger length = [str length];
    //         RGB            RGBA          RRGGBB        RRGGBBAA
    if (length != 3 && length != 4 && length != 6 && length != 8) {
        return NO;
    }
    
    //RGB,RGBA,RRGGBB,RRGGBBAA
    if (length < 5) {
        *r = hexStrToInt([str substringWithRange:NSMakeRange(0, 1)]) / 255.0f;
        *g = hexStrToInt([str substringWithRange:NSMakeRange(1, 1)]) / 255.0f;
        *b = hexStrToInt([str substringWithRange:NSMakeRange(2, 1)]) / 255.0f;
        if (length == 4)  *a = hexStrToInt([str substringWithRange:NSMakeRange(3, 1)]) / 255.0f;
        else *a = 1;
    } else {
        *r = hexStrToInt([str substringWithRange:NSMakeRange(0, 2)]) / 255.0f;
        *g = hexStrToInt([str substringWithRange:NSMakeRange(2, 2)]) / 255.0f;
        *b = hexStrToInt([str substringWithRange:NSMakeRange(4, 2)]) / 255.0f;
        if (length == 8) *a = hexStrToInt([str substringWithRange:NSMakeRange(6, 2)]) / 255.0f;
        else *a = 1;
    }
    return YES;
}

+ (instancetype)xes_colorWithHexString:(NSString *)hexStr {
    CGFloat r, g, b, a;
    if (hexStrToRGBA(hexStr, &r, &g, &b, &a)) {
        return [UIColor colorWithRed:r green:g blue:b alpha:a];
    }
    return nil;
}


#pragma mark - 主题色
// 主题色(网校红) 0xE02727
+ (UIColor *)wx_ThemeColor
{
    return [UIColor wx_ThemeColorWithAlpha:1.0];
}


// 主题色(网校红)
+ (UIColor *)wx_ThemeColorWithAlpha:(CGFloat)alpha
{
    return [self wx_colorWithHEXValue:0xe02727 alpha:alpha];
}

// 字体灰
+(UIColor *)wx_FontGrayColor
{
    return [self wx_colorWithHEXValue:0x333333];
}

// 字体浅灰
+(UIColor *)wx_FontLightGrayColor
{
    return [self wx_colorWithHEXValue:0x666666];
}

// 字体微灰 0x999999
+ (UIColor *)wx_FontLessGrayColor
{
    return [self wx_colorWithHEXValue:0x999999];
}

// 背景灰
+(UIColor *)wx_BackGroundGrayColor
{
    return [self wx_colorWithHEXValue:0xEEEEEE];
}

@end
